package com.product.system.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.product.system.domain.response.ArgpMachining;
import com.product.system.service.ArgpMachiningService;

import java.util.List;

public class ArgpMachiningDataListener implements ReadListener<ArgpMachining> {


    private static final int BATCH_COUNT = 5;


    private ArgpMachiningService argpMachiningService;

    public ArgpMachiningDataListener(ArgpMachiningService argpMachiningService) {
        this.argpMachiningService = argpMachiningService;
    }

    private List<ArgpMachining> cachedArgpMachiningList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);



    @Override
    public void invoke(ArgpMachining argpMachining, AnalysisContext analysisContext) {


        cachedArgpMachiningList.add(argpMachining);
        //插入完之后清空缓存
        if (cachedArgpMachiningList.size()>=BATCH_COUNT) {
            argpMachiningService.loadinRoleMatchAdd(cachedArgpMachiningList);
            cachedArgpMachiningList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("数据解析成功");
        //剩余的数据开始批量添加
        if (cachedArgpMachiningList.size()>0) {
            argpMachiningService.loadinRoleMatchAdd(cachedArgpMachiningList);
        }
    }
}
